<?php
// @formatter:off
/**
 * @file c.tag.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.tag.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0.0.1/themes/default/controlers/contents/c.tag.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_TAG_VIEW)) {
	require_once "{$this->_themeRoot}/controlers/generics/comicpreview.php";
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcAction;
	global $wcTag;
	global $wctDefaults;
	global $wcThemeAssigns;

	if($wcTag && $wcTag->ok()) {
		$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
		$minCmcInPage = $wctDefaults["max-per-page"]["tag"] * ($page - 1);
		$maxCmcInPage = $minCmcInPage + $wctDefaults["max-per-page"]["tag"];

		$wcThemeAssigns["PATH"] = "{$wcTag->name}";
		$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.tag.html";
		$wcThemeAssigns["INFO-NAME"] = $wcTag->name;
		$wcThemeAssigns["INFO-COUNT"] = $wcTag->count;

		if($wcGroup->isPublic()) {
			$wcThemeAssigns["RSS-LINK"] = "rss.php?group={$wcGroup->id()}&action=".WC_RSS_TAG."&tag=".$wcTag->id();
			$wcThemeAssigns["RSS-TITLE"] = "Tag '{$wcTag->name()}' Feeds";
		} elseif($wcSession->isLogged()) {
			$wcThemeAssigns["RSS-LINK"] = "rss.php?group={$wcGroup->id()}&action=".WC_RSS_TAG."&tag={$wcTag->id()}&uhash={$wcUser->rssHash()}";
			$wcThemeAssigns["RSS-TITLE"] = "Tag '{$wcTag->name()}' Feeds";
		}

		if(!$wcTag->isSpecial() && $wcProfile->allowedTo(WC_PERM_TAG_RENAME)) {
			$wcThemeAssigns["INFO-NAME-EDITABLE"] = true;
		}

		$wcThemeAssigns["comics"] = array();
		$comicCount = 0;
		$items = $wcTag->membersFull();
		uasort($items, "WCComic::NameSort");
		foreach($items as $comic) {
			if($comic->ok()) {
				if($comicCount >= $minCmcInPage && $comicCount < $maxCmcInPage) {
					$wcThemeAssigns["comics"][] = wcGetComicPreviewAssigns($comic);
				}
				$comicCount++;
			}
		}

		wcSetPager(requestParams(true, $wcAction, array('page')), ceil($comicCount / $wctDefaults["max-per-page"]["tag"]), $page);

		if($wcTag->ok() && !$wcTag->valid_preview) {
			$wcTag->generatePreviews();
		}
	} else {
		$wcThemeAssigns["error-string"] = "Invalid tag";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to open tags";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>